<?php

namespace Modules\Order\Http\Controllers\Client\V1;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

use Modules\Order\Entities\ProductOrder as Order;

use Validator;
use ThrowException;
use Auth;

class OrderCommentController extends Controller
{
    public function skus(Request $req, $uuid)
    {
        $user = Auth::requireLoginUser();

        $order = $user->orders()->where('uuid', $uuid)->first() ?: ThrowException::Conflict('找不到此订单');

        $list = [];
        foreach ($order->skus as $orderSku) {
            array_push($list, [
                'title' => $orderSku->title,
                'thumb' => $orderSku->thumb,
                'price' => $orderSku->price,
                'selling_price' => $orderSku->selling_price,
                'attrs' => $orderSku->attrs
            ]);
        }

        return [
            'list' => $list
        ];
    }

    public function store(Request $req, $uuid)
    {
        $rule = [
        ];

        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest();

        $user = Auth::requireLoginUser();

        $options = $req->all();
        $options['with'] = ['skus'];
        $options['where'] = [
            'user_id' => $user->id,
            'user_deleted_at' => NULL
        ];

        $options['only'] = [
            'id',
            'uuid',
            'number',
            'user_remark',
            'pay_price',
            'created_at',
            'product_total',
            'sku_total',
            'union_status',
            'union_status_text',
            'auto_closed_at',
            'skus' => [
                '*' => [
                    'title',
                    'thumb',
                    'price',
                    'selling_price',
                    'attrs',
                    'total'
                ]
            ]
        ];

        $list = Order::getList($options);

        return $list->generateListResponse();
    }
}
